package com.hxht.storage.server.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.persistence.TypedQuery;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.hxht.storage.server.dao.ManualRecordDao;
import com.hxht.storage.server.pojo.VideoManualRecord;

@Repository
public class ManualRecordDaoImpl extends BaseDaoImpl<VideoManualRecord>
								 implements ManualRecordDao{

	@PostConstruct
	public void init(){
		clazz = VideoManualRecord.class;
	}

	@Override
	public List<VideoManualRecord> getRecordByDevice(String deviceId, int channelIndex,
			String userId, String marks, Date start, Date stop, Integer type) {
		// TODO Auto-generated method stub
		List<VideoManualRecord> recordList = new ArrayList<VideoManualRecord>();
		
		 StringBuilder queryString = new StringBuilder("from VideoManualRecord where "
				+ "deviceId=:deviceId and deviceChannel=:channel");
		
		 if (start != null){
			 // 只查找 录像时间片段 和 搜索时间段 有重复的 录像记录
			 queryString.append(" and not ( startTime >= :stop or (stopTime != null and stopTime <= :start) )");
		 }
		 
		 // 查找 录像类型
		 if (type != null){
			 queryString.append(" and manualRecordType =:type");
		 }
		 
		 if (!StringUtils.isEmpty(userId)){
			queryString.append(" and userId=:userId");
		}
		
		// marks 格式如 mark1,mark2,mark3
		String[] markArray = null;
		if (!StringUtils.isEmpty(marks)){
			markArray = marks.split("\\s*,\\s*");

			queryString.append(" and mark in (");
			for (int i = 1; i <= markArray.length; i++){
				queryString.append(" :mark" + i + ",");
			}
			queryString.deleteCharAt(queryString.length()-1);
			queryString.append(")");
		}
		
		TypedQuery<VideoManualRecord> query = entityManage.createQuery(
				queryString.toString(), clazz).setParameter("deviceId", deviceId).setParameter("channel", channelIndex);
		
		if (start != null){
			query.setParameter("start", start).setParameter("stop", stop);
		}
		
		if (!StringUtils.isEmpty(userId)){
			query.setParameter("userId", userId);
		}
		
		 if (type != null){
			 query.setParameter("type", type);
		 }
		 
		
		if (!StringUtils.isEmpty(marks)){
			 for (int i = 1; i <= markArray.length; i++)
             {
                 query.setParameter("mark" + i, markArray[i-1]);
             }
		}
		
		recordList = query.getResultList();
		
		return recordList;
	}

	@Override
	public VideoManualRecord getRecodingDevice(String deviceId, int channelIndex) {
		// TODO Auto-generated method stub
		List<VideoManualRecord> recordList;
		
		TypedQuery<VideoManualRecord> query = entityManage.createQuery(
				"from VideoManualRecord where deviceId=:deviceId and deviceChannel=:channel and isRecoding=:recording", clazz)
				.setParameter("deviceId", deviceId).setParameter("channel", channelIndex).setParameter("recording", true);
		
		recordList = query.getResultList();
		
		if (recordList != null && recordList.size() != 0){
			return recordList.get(0);
		} else {
			return null;
		}
	}

	@Override
	public VideoManualRecord getRecodingByIpchannel(String nvrIp, int ipchannel) {
		
		List<VideoManualRecord> recordingList;
		
		TypedQuery<VideoManualRecord> query = entityManage.createQuery(
				"select record from VideoManualRecord as record left join record.ipChannel as ipchannel"
				+ " left join ipchannel.nvrDevice as nvr where nvr.ip=:nvrIp and "
				+ "ipchannel.dvrChannel=:channel and record.isRecoding=:recording", clazz)
				.setParameter("nvrIp", nvrIp).setParameter("channel", ipchannel).setParameter("recording", true);
		
		recordingList = query.getResultList();
		
		if (recordingList != null && recordingList.size() != 0){
			return recordingList.get(0);
		} else {
			return null;
		}
	}

	@Override
	public List<VideoManualRecord> getRecordByMark(String mark) {
		
		List<VideoManualRecord> recordList;
		
		TypedQuery<VideoManualRecord> query = entityManage.createQuery(
				"from VideoManualRecord where mark=:mark", clazz)
				.setParameter("mark", mark);
		
		recordList = query.getResultList();
		
		return recordList;
	}

	@Override
	public VideoManualRecord getRecordByUuid(String recordUuid) {
		
		VideoManualRecord record = null;
		
		TypedQuery<VideoManualRecord> query = entityManage.createQuery(
				"from VideoManualRecord where uuid=:uuid", clazz)
				.setParameter("uuid", recordUuid);
		
		List<VideoManualRecord> recordList = query.getResultList();
		
		if (recordList != null && recordList.size() != 0){
			record = recordList.get(0);
		}
		
		return record;
	}

	@Override
	public List<VideoManualRecord> getRecodingDevice() {
		// TODO Auto-generated method stub
		List<VideoManualRecord> recordList = null;
		
		TypedQuery<VideoManualRecord> query = entityManage.createQuery(
				"from VideoManualRecord where isRecoding=:recording", clazz)
				.setParameter("recording", true);
		
		recordList = query.getResultList();
		
		return recordList;
	}
	
}
